package com.gec.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gec.common.R;
import com.gec.dto.DishDto;
import com.gec.dto.SetmealDto;
import com.gec.entity.Setmeal;
import com.gec.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @author : [chenxys]
 * @version : [v1.0]
 * @createTime : [2022/7/5 17:32]
 */
@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    /**
     * 分页查询
     * @author chenxys
     * @param page 当前页
     * @param pageSize 每页显示条数
     * @param name 查询条件
     * @return com.gec.common.R<com.baomidou.mybatisplus.extension.plugins.pagination.Page>
     */
    @GetMapping("/page")
    public R<Page> page(Long page,Long pageSize,
                        @RequestParam(required = false) String name){
        log.info("套餐分页查询");
        Page<Setmeal> pageInfo = new Page<>(page, pageSize);
        setmealService.selectByCondition(pageInfo,name);
        return R.success(pageInfo);
    }

    /**
     * 查询单个套餐所有信息
     * @author chenxys
     * @param id 套餐ID
     * @return com.gec.common.R<com.gec.dto.SetmealDto>
     */
    @GetMapping("/{id}")
    public R<SetmealDto> getOne(@PathVariable("id") Long id){
        SetmealDto setmealDto = setmealService.getOneById(id);
        log.info("查询单个套餐所有信息");
        return R.success(setmealDto);
    }

    /**
     * 更新套餐
     * @author chenxys
     * @param setmealDto 套餐信息
     * @return com.gec.common.R<java.lang.String>
     */
    @PutMapping
    @CacheEvict(value = "setmealCache", allEntries = true)
    public R<String> update(@RequestBody SetmealDto setmealDto){
        setmealService.updateWithDish(setmealDto);
        log.info("更新套餐信息");
        return R.success("更新成功");
    }

    /**
     * 更新套餐状态 启售/停售
     * @author chenxys
     * @param code 状态
     * @param ids 套餐ID集合
     * @return com.gec.common.R<java.lang.String>
     */
    @PostMapping("/status/{code}")
    @CacheEvict(value = "setmealCache", allEntries = true)
    public R<String> status(@PathVariable("code") Integer code,Long[] ids){
        setmealService.updateWithCode(code,ids);
        log.info("更新套餐状态");
        return R.success("更新套餐状态成功");
    }

    /**
     * 批量删除套餐
     * @author chenxys
     * @param ids 套餐ID
     * @return com.gec.common.R<java.lang.String>
     */
    @DeleteMapping
    @CacheEvict(value = "setmealCache", allEntries = true)
    public R<String> delete(Long[] ids){
        if (!ObjectUtils.isEmpty(ids)){
            setmealService.removeWithDish(Arrays.asList(ids));
            return R.success("删除套餐成功");
        }
        log.info("删除套餐");
        return R.error("未选中套餐");
    }

    /**
     * 保存新套餐
     * @author chenxys
     * CacheEvict 清除缓存
     * @param setmealDto 套餐信息
     * @return com.gec.common.R<java.lang.String>
     */
    @PostMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> save(@RequestBody SetmealDto setmealDto){
        setmealService.saveWithDish(setmealDto);
        return R.success("保存成功");
    }

    /**
     * 查询套餐信息
     * Cacheable 开启缓存 --> 自动缓存到redis reids中有数据就查redis 无数据就查数据库
     * @author chenxys
     * @param setmeal 套餐
     * @return com.gec.common.R<java.util.List<com.gec.entity.Setmeal>>
     */
    @GetMapping("/list")
    @Cacheable(value = "setmealCache", key = "#setmeal.categoryId + '_' + #setmeal.status")
    public R<List<Setmeal>> list(Setmeal setmeal){
        List<Setmeal> setmealList = setmealService.listBySetmeal(setmeal);
        return R.success(setmealList);
    }

    /**
     * 获取当前套餐下所有菜品信息
     * @author chenxys
     * @param setmealId 套餐ID
     * @return com.gec.common.R<java.util.List<com.gec.dto.DishDto>>
     */
    @GetMapping("/dish/{id}")
    public R<List<DishDto>> dishList(@PathVariable("id") Long setmealId){
        List<DishDto> dishDtoList = setmealService.dishList(setmealId);
        return R.success(dishDtoList);
    }
}
